local skel = fk.CreateSkill {
  name = "rmt__huaijing",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards == 0 or table.contains(player:getTableMark("@rmt__huaijing-round"), "huaijing__inflict") then return end
    local card = Fk:cloneCard("duel")
    card.skillName = self.name
    card:addSubcards(cards)
    return card
  end,
  can_use = function (self, player)
    return not player:isNude() and not table.contains(player:getTableMark("@rmt__huaijing-round"), "huaijing__inflict")
  end,
})

skel:addEffect(fk.Damage, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    if player == target then
      player.room:addTableMarkIfNeed(player, "@rmt__huaijing-round", "huaijing__cause")
    elseif player == data.to then
      player.room:addTableMarkIfNeed(player, "@rmt__huaijing-round", "huaijing__inflict")
    end
  end
})

skel:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and not table.contains(player:getTableMark("@rmt__huaijing-round"), "huaijing__cause")
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(2, skel.name)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  if is_start then return end
  local one = false
  local two = false
  player.room.logic:getEventsOfScope(GameEvent.Damage, 999, function (e)
    if e.data.to == player then
      one = true
    end
    if e.data.from == player then
      two = true
    end
    return false
  end, Player.HistoryRound)
  if one then
    player.room:addTableMarkIfNeed(player, "@rmt__huaijing-round", "huaijing__cause")
  end
  if two then
    player.room:addTableMarkIfNeed(player, "@rmt__huaijing-round", "huaijing__inflict")
  end
end)

Fk:loadTranslationTable{
  ["rmt__huaijing"] = "怀靖",
  [":rmt__huaijing"] = "若你本轮未：受到过伤害，你可以将一张牌当【决斗】使用；造成过伤害，你受到伤害后摸两张牌。",

  ["@rmt__huaijing-round"] = "怀靖",
  ["huaijing__cause"] = "造",
  ["huaijing__inflict"] = "受",

  ["$rmt__huaijing1"] = " ",
  ["$rmt__huaijing2"] = " ",
}

return skel
